package com.cake.liulanxiangzhu.controller;


import com.cake.liulanxiangzhu.mapper.AdminUserMapper;
import com.cake.liulanxiangzhu.pojo.dto.AdminUserDTO;
import com.cake.liulanxiangzhu.pojo.entity.User;
import com.cake.liulanxiangzhu.security.LoginPrincipal;
import com.cake.liulanxiangzhu.service.AdminUserService;
import com.cake.liulanxiangzhu.service.IWxLogin;
import com.cake.liulanxiangzhu.web.JsonResult;
//import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Validated
@Slf4j
@RequestMapping("/user")
public class AdminLoginController {
    @Autowired
    AdminUserMapper mapper;

    @Autowired
    IWxLogin service;

    @Autowired
    AdminUserService adminUserService;

    //    @PostMapping("/login")
//    public int login(User user, HttpSession session, HttpServletResponse response) {
//        System.out.println("user = " + user + ", session = " + session);
//        User u = mapper.selectByUsername(user.getUsername());
//        if (u != null) {
//            if (u.getPassword().equals(user.getPassword())) {
//                Cookie c1 = new Cookie("username", user.getUsername());
//                c1.setPath("/");
//                c1.setHttpOnly(false);
//                response.addCookie(c1);
//                session.setAttribute("user", u);
//                return 1;
//            }
//            return 3;
//        }
//        return 2;
//    }
    @PostMapping("/login")
    public JsonResult<String> login(AdminUserDTO adminUserDTO) {
        String Authorization = adminUserService.loginSystem(adminUserDTO);
        return JsonResult.ok(Authorization);
    }

    @PostMapping("/smsLogin")
    public JsonResult<String> smsLogin(AdminUserDTO adminUserDTO) {
        String Authorization = adminUserService.loginBySms(adminUserDTO);
        return JsonResult.ok(Authorization);
    }

    @PostMapping("/weiXinLogin")
    public JsonResult<String> weiXinLogin(String Code) {
        log.debug("传入的Code：{}",Code);
        String Authorization = adminUserService.loginByWeiXin(Code);
        return JsonResult.ok(Authorization);
    }

    @PostMapping("/getRandomNum")
    public JsonResult<String> getRandomNum(Long phone) {
        String randomNum = adminUserService.randomNum(phone);
        return JsonResult.ok(randomNum);
    }


    @PostMapping("/logout")
    public JsonResult<Void> logout() {
        adminUserService.logoutSystem();
        return JsonResult.ok();
    }

    @PostMapping("/add-new")
    public int addNew(@RequestBody User user, HttpSession session) {
        System.out.println("user = " + user + ", session = " + session);
        User user1 = ((User) session.getAttribute("user"));
        System.out.println(user1);
        if (user1 == null) {
            return 2;
        } else {
            return 1;
        }
    }

    @GetMapping("/get-new")
    @PreAuthorize("hasAuthority('read')")
    public List<String> getNew() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginPrincipal principal = (LoginPrincipal) authenticationToken.getPrincipal();
        log.debug("当事人：{}", principal);
        List<String> name = mapper.select();
        return name;
    }

    @GetMapping("/getToken")
    public JsonResult<String> getUserInfo(@RequestParam("code") String code) {
        String token = service.queryToken(code);
        return JsonResult.ok(token);
    }

    @PostMapping("/test")
    public String test(HttpServletRequest request) {
        String test = service.test(request);
        return test;

    }


}



